﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Administrator
{
    public partial class CustomSectionList : BasePage
    {
        CustomSectionLogic customSectionLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowCustomSections();
            }
        }

        private void ShowCustomSections()
        {
            IEnumerable<CustomSection> list = customSectionLogic.GetList().OrderBy(d => d.Id);
            this.GridViewCatalogue.DataSource = list;
            this.GridViewCatalogue.DataBind();
        }

        private void ClearControls()
        {
            ClearControlText(this.txtId, this.txtTitle, this.Summary);
            this.btnSaveCustomSection.CommandArgument = EditStateEnum.New.ToString();
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            LinkButton linkbtn = sender as LinkButton;
            if (linkbtn != null)
            {
                string strId = linkbtn.CommandArgument;
                int id = 0;
                if (int.TryParse(strId, out id))
                {
                    btnSaveCustomSection.CommandArgument = EditStateEnum.Edit.ToString();
                    CustomSection catalogue = customSectionLogic.GetList(p => p.Id == id).FirstOrDefault();
                    this.txtTitle.Text = catalogue.Title;
                    this.Summary.Text = catalogue.Content;
                    this.txtId.Value = catalogue.Id.ToString();
                }
            }
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            LinkButton linkbtn = sender as LinkButton;
            if (linkbtn != null)
            {
                string strId = linkbtn.CommandArgument;
                int id = 0;
                if (int.TryParse(strId, out id))
                {
                    CustomSection catalogue = customSectionLogic.GetList(p => p.Id == id).FirstOrDefault();
                    if (catalogue != null)
                    {
                        customSectionLogic.Delete(catalogue);
                        ShowCustomSections();
                    }
                }
            }
        }

        private CustomSection GetCustomSectionForSave()
        {
            CustomSection result = null;

            string str = btnSaveCustomSection.CommandArgument;
            if (str == EditStateEnum.Edit.ToString())
            {
                int id = 0;
                if (int.TryParse(this.txtId.Value, out id))
                {
                    result = customSectionLogic.GetList(p => p.Id == id).FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                result = new CustomSection();
                result.Id = customSectionLogic.GetNewIdentity();
                result.CreatedTime = DateTime.Now;
            }


            result.Title = txtTitle.Text;
            result.Content = Summary.Text;

            return result;
        }

        protected override void InitLogic()
        {
            customSectionLogic = new CustomSectionLogic();
        }

        protected override void DisposeLogic()
        {
            if (customSectionLogic != null)
            {
                customSectionLogic.Dispose();
            }
        }

        protected void btnSaveCustomSection_Click(object sender, EventArgs e)
        {
            try
            {
                CustomSection catalogue = GetCustomSectionForSave();
                customSectionLogic.Save(catalogue);
                AddScript("alert('保存自定义内容成功！');");
                ShowCustomSections();
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('保存自定义内容失败！');");
            }
            finally
            {
                ClearControls();
            }
        }
    }
}